<template>
  <header class="header" :class="{ 'header-scrolled': isScrolled }">
    <div class="header-container">
      <!-- Logo 区域 -->
      <router-link to="/" class="logo">
        <img src="../assets/logo.svg" alt="Logo" class="logo-img">
        <span class="logo-text">大成瑄辉</span>
      </router-link>

      <!-- 导航菜单 -->
      <nav class="nav-menu">
        <router-link 
          v-for="item in menuItems" 
          :key="item.path" 
          :to="item.path"
          class="nav-item"
          :class="{ 'active': $route.path === item.path }"
        >
          <i :class="item.icon"></i>
          {{ item.name }}
        </router-link>
      </nav>

      <!-- 右侧功能区 -->
      <div class="header-right">
        <!-- 搜索按钮 -->
        <button class="icon-button" @click="toggleSearch">
          <i class="fas fa-search"></i>
        </button>

        <!-- 主题切换 -->
        <button class="icon-button" @click="toggleTheme">
          <i :class="isDark ? 'fas fa-sun' : 'fas fa-moon'"></i>
        </button>

        <!-- 用户菜单 -->
        <div class="user-menu" v-if="isLoggedIn">
          <img :src="userAvatar" alt="User" class="user-avatar">
        </div>
        <button v-else class="login-button" @click="handleLogin">
          登录
        </button>
      </div>
    </div>

    <!-- 搜索框 -->
    <div class="search-container" :class="{ 'search-active': isSearchActive }">
      <div class="search-box">
        <i class="fas fa-search"></i>
        <input 
          type="text" 
          placeholder="搜索..." 
          v-model="searchQuery"
          @keyup.enter="handleSearch"
        >
        <button class="close-search" @click="toggleSearch">
          <i class="fas fa-times"></i>
        </button>
      </div>
    </div>
  </header>
</template>

<script>
export default {
  name: 'TheHeader',
  data() {
    return {
      isScrolled: false,
      isSearchActive: false,
      searchQuery: '',
      isDark: false,
      isLoggedIn: false,
      userAvatar: '',
      menuItems: [
        { name: '首页', path: '/', icon: 'fas fa-home' },
        { name: '博客', path: '/blog', icon: 'fas fa-blog' },
        { name: '树洞', path: '/treehole', icon: 'fas fa-comment-dots' },
        { name: 'AI对话', path: '/ai-chat', icon: 'fas fa-robot' },
        { name: '游戏', path: '/game', icon: 'fas fa-gamepad' },
        { name: '关于', path: '/about', icon: 'fas fa-info-circle' },
        { name: '联系', path: '/contact', icon: 'fas fa-envelope' },
        { name: '大事记', path: '/timeline', icon: 'fas fa-history' },
        { name: '时间胶囊beta', path: '/time-mail', icon: 'fas fa-hourglass-half' }
      ]
    }
  },
  mounted() {
    window.addEventListener('scroll', this.handleScroll)
    // 从 localStorage 读取主题设置
    const savedTheme = localStorage.getItem('theme')
    this.isDark = savedTheme === 'dark'
    if (this.isDark) {
      document.documentElement.classList.add('dark-mode')
    }
  },
  beforeUnmount() {
    window.removeEventListener('scroll', this.handleScroll)
  },
  methods: {
    handleScroll() {
      this.isScrolled = window.scrollY > 20
    },
    toggleSearch() {
      this.isSearchActive = !this.isSearchActive
      if (this.isSearchActive) {
        this.$nextTick(() => {
          document.querySelector('.search-box input').focus()
        })
      }
    },
    handleSearch() {
      if (this.searchQuery.trim()) {
        // 实现搜索逻辑
        console.log('Searching for:', this.searchQuery)
      }
    },
    toggleTheme() {
      this.isDark = !this.isDark
      document.documentElement.classList.toggle('dark-mode')
      localStorage.setItem('theme', this.isDark ? 'dark' : 'light')
    },
    handleLogin() {
     this.$router.push('/auth')
    }
  }
}
</script>

<style scoped>
.header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  transition: all 0.3s ease;
  background: rgba(255, 255, 255, 0.8);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.header-scrolled {
  background: rgba(255, 255, 255, 0.95);
  box-shadow: 0 2px 20px rgba(0, 0, 0, 0.1);
}

.header-container {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 20px;
  height: 70px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

/* Logo 样式 */
.logo {
  display: flex;
  align-items: center;
  text-decoration: none;
  color: var(--text-color);
  transition: transform 0.3s ease;
}

.logo:hover {
  transform: translateY(-2px);
}

.logo-img {
  height: 40px;
  margin-right: 10px;
}

.logo-text {
  font-size: 1.2em;
  font-weight: 600;
  background: linear-gradient(45deg, var(--primary-color), var(--accent-color));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

/* 导航菜单样式 */
.nav-menu {
  display: flex;
  gap: 30px;
}

.nav-item {
  text-decoration: none;
  color: var(--text-color);
  font-weight: 500;
  padding: 8px 0;
  position: relative;
  transition: color 0.3s ease;
}

.nav-item i {
  margin-right: 6px;
  font-size: 0.9em;
}

.nav-item::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 0;
  height: 2px;
  background: var(--primary-color);
  transition: width 0.3s ease;
}

.nav-item:hover::after,
.nav-item.active::after {
  width: 100%;
}

.nav-item:hover,
.nav-item.active {
  color: var(--primary-color);
}

/* 右侧功能区样式 */
.header-right {
  display: flex;
  align-items: center;
  gap: 20px;
}

.icon-button {
  background: none;
  border: none;
  color: var(--text-color);
  font-size: 1.2em;
  cursor: pointer;
  padding: 8px;
  border-radius: 50%;
  transition: all 0.3s ease;
}

.icon-button:hover {
  background: rgba(0, 0, 0, 0.05);
  transform: translateY(-2px);
}

.login-button {
  background: var(--primary-color);
  color: white;
  border: none;
  padding: 8px 20px;
  border-radius: 20px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.user-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  cursor: pointer;
  transition: transform 0.3s ease;
}

.user-avatar:hover {
  transform: scale(1.1);
}

/* 搜索框样式 */
.search-container {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 70px;
  background: var(--card-bg);
  transform: translateY(-100%);
  transition: transform 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}

.search-active {
  transform: translateY(0);
}

.search-box {
  width: 100%;
  max-width: 600px;
  display: flex;
  align-items: center;
  padding: 0 20px;
  gap: 10px;
}

.search-box input {
  flex: 1;
  border: none;
  background: none;
  font-size: 1.1em;
  color: var(--text-color);
  padding: 10px;
}

.search-box input:focus {
  outline: none;
}

.close-search {
  background: none;
  border: none;
  color: var(--text-color);
  cursor: pointer;
  padding: 8px;
  border-radius: 50%;
  transition: all 0.3s ease;
}

.close-search:hover {
  background: rgba(0, 0, 0, 0.05);
}

/* 暗色主题适配 */
:root[class~="dark-mode"] .header {
  background: rgba(30, 30, 30, 0.8);
}

:root[class~="dark-mode"] .header-scrolled {
  background: rgba(30, 30, 30, 0.95);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .nav-menu {
    display: none;
  }

  .header-container {
    padding: 0 15px;
  }

  .logo-text {
    display: none;
  }
}
</style> 